<!-- 选项表单弹窗 -->
<form id="question-option-form" lay-filter="question-option-form" class="layui-form model-form">
    <input name="quesid" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">选项</label>
        <div class="layui-input-block">
            <select name="optionIds" xm-select="optionIds">
            </select>
        </div>
    </div>

    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="question-option-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'], function () {
        let layer = layui.layer;
        let admin = layui.admin;
        let form = layui.form;
        let formSelects = layui.formSelects;

        let question = admin.getTempData('t_questionOption');

        // 获取所有选项
        layer.load(2);
        admin.req('api-paper/option/' + question.quesid + '/getAllOptions', {}, function (result) {
            let data = result.data;
            layer.closeAll('loading');
            let questionSelectData = [];
            for (let i = 0; i < data.length; i++) {
                questionSelectData.push({name: data[i].name, value: data[i].id});
            }
            formSelects.data('optionIds', 'local', {arr: questionSelectData});
            // 回显数据
            $('#question-option-form').attr('method', 'POST');
            form.val('question-option-form', question);

            let rds = [];
            for (let i = 0; i < data.length; i++) {
                if (data[i].checked) {
                    rds.push(data[i].id);
                }
            }
            formSelects.value('optionIds', rds);
        }, 'GET');

        // 表单提交事件
        form.on('submit(question-option-form-submit)', function (data) {
            layer.load(2);
            let params = {
                quesid: data.field.quesid,
                optionIds: data.field.optionIds.split(",")
            };
            admin.req('api-paper/option/addOptions', JSON.stringify(params), function (data) {
                layer.closeAll('loading');
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }

            }, 'POST');
            return false;
        });
    });
</script>